package com.biz.primus.ms.base.jpa.converter;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.util.Objects;

public abstract class EnumsSetNameStringConverter<E extends Enum> extends AbstractSetStringConverter<E> {

//    public static final EnumsSetNameStringConverter INSTANCE = new EnumsSetNameStringConverter();

    @Override
    protected E string2Element(String data) {
        @SuppressWarnings("unchecked") Class<E> clazz = (Class<E>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        try {
            @SuppressWarnings("unchecked") E[] es = (E[]) clazz.getMethod("values").invoke(null);
            for (E e : es) {
                if(Objects.equals(data, e.toString())){
                    return e;
                }
            }

        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

}
